// pages/getcoupon/getcoupon.js
var server = require('../../utils/server.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    class_list:[],
    coupon_list:[],
    share:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // server.postJSON('/seller/headcategroy', {}, res => {
    //   // console.log(res)
    //   if (res.data.status == 200) {
    //     this.setData({
    //       class_list: res.data.list
    //     })
    //     this.onChange();
    //   }
    // })
    server.send('/seller/headcategroy','POST',{}).then(res=>{
      if (res.status == 200) {
        this.setData({
          class_list: res.list
        })
        this.onChange();
      }
    })
  },

  onChange: function(e){
    // console.log(e)
    let that = this
    let id='';
    if(e){
        let index = e.detail.index
       id = that.data.class_list[index].id
    }
    // console.log(id)
    let _params={
      username: getApp().globalData.username,
      token: getApp().globalData.token,
      site_id: getApp().globalData.site_id,
      cat_id:id
    }
    // server.postJSON('/sellercoupons/coupons-center',_params,(res)=>{
    //     // console.log(res)
    //     that.setData({
    //       coupon_list: res.data.list.coupons_list
    //     })
    // })
    server.send('/sellercoupons/coupons-center','POST',_params).then(res=>{
      that.setData({
          coupon_list: res.list.coupons_list
        })
    })
  },
  getcou:function(e){
    let index = e.currentTarget.dataset.index;
    let that=this;
    let _params={
      username: getApp().globalData.username,
      token: getApp().globalData.token,
      coupons_id:index,
      share:that.data.share
    };
    server.send('/sellercoupons/coupons-get','POST',_params).then(res=>{
        wx.showToast({
          title:res.msg,
          mask:true,
          icon:'none'
        })
        if(res.msg == '领取成功'){
          that.onChange();
          that.setData({
            active:0,
            share:0,
          })
        }
    })
  },
  goseller:function(e){
    let seller_id = e.currentTarget.dataset.seller_id
    wx:wx.navigateTo({
      url: '../storedtl/storedtl?objectId=' + seller_id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this
    return {
      title: '用户每天可以在领券中心分享来领取优惠券',
      desc: '分享领取优惠券',
      path: '/pages/getcoupon/getcoupon',
      success: res => {
        that.setData({
          share: 1,          
        })
      }
    }
  }
})